package edu.aynu.controller;

import edu.aynu.entity.Course;
import edu.aynu.service.CourseService;
import edu.aynu.util.Result;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.annotation.Resource;

/**
 * (Course)表控制层
 *
 * @author haha
 * @since 2021-06-02 12:50:05
 */
@RestController
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("course")
    public Result selectOne(Integer id) {
        Course course = this.courseService.queryById(id);
        if (course == null) {
            return Result.fail();
        }
        return Result.success().add("course", course);
    }

    /**
     * 通过实体作为筛选条件查询
     */
    @GetMapping("courses")
    public Result queryAll(Course course) {
        List<Course> list = this.courseService.queryAll(course);
        if (list.size() == 0) {
            return Result.fail();
        }
        return Result.success().add("courses", list);
    }

    /**
     * 新增数据
     */
    @PostMapping("course")
    public Result insert(@RequestBody Course course) {
        int i = this.courseService.insert(course);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 修改数据
     */
    @PutMapping("course")
    public Result update(@RequestBody Course course) {
        int i = this.courseService.update(course);
        if (i == 0) {
            return Result.fail();
        }
        return Result.success();
    }

    /**
     * 通过主键删除数据
     *
     * @return 是否成功
     */
    @DeleteMapping("course")
    public Result deleteById(Integer id) {
        boolean b = this.courseService.deleteById(id);
        if (b == false) {
            return Result.fail();
        }
        return Result.success();
    }
}
